/***************************************************************************
 *   Copyright © 2010 by Nikolay Kornaev                                   *
 *   n.kornaev@gmail.com                                                   *
 *                                                                         *
 *                    This file is part of neuma.                          *
 *                                                                         *
 *   neuma is free software; you can redistribute it and/or modify it      *
 *   under the terms of the GNU General Public License as published by     *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   neuma is distributed in the hope that it will be useful,              *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, see <http://www.gnu.org/licenses/>   *
 ***************************************************************************/

#include <QtGui>

#include "SyntaxHighlighter.h"

namespace Neuma
{
  NeumaSyntaxHighlighter::NeumaSyntaxHighlighter(QTextDocument *parent) :
    QSyntaxHighlighter(parent)
  {
    HighlightingRule rule;
    rxInitNeuma = QRegExp("^#");
    rxInitSyllabe = QRegExp("^&");
    rxErrorInsertedStart = QRegExp("^>>");

    fLineNeumas.setBackground(Qt::white);
    rule.pattern = QRegExp(rxInitNeuma.pattern() + ".*$", Qt::CaseInsensitive);
    rule.format = fLineNeumas;
    highlightingRules.append(rule);

    fLineSyllabes.setBackground(Qt::white);
    rule.pattern = QRegExp(rxInitSyllabe.pattern() + ".*$", Qt::CaseInsensitive);
    rule.format = fLineSyllabes;
    highlightingRules.append(rule);

    fInitNeuma.setForeground(Qt::darkGreen);
    fInitNeuma.setBackground(fLineNeumas.background());
    fInitNeuma.setFontWeight(QFont::Bold);
    rule.pattern = rxInitNeuma;
    rule.format = fInitNeuma;
    highlightingRules.append(rule);

    fInitSyllabe.setForeground(Qt::darkBlue);
    fInitSyllabe.setBackground(fLineSyllabes.background());
    fInitSyllabe.setFontWeight(QFont::Bold);
    rule.pattern = rxInitSyllabe;
    rule.format = fInitSyllabe;
    highlightingRules.append(rule);

    fErrorInserted.setForeground(Qt::red);
    fErrorInserted.setUnderlineStyle(QTextCharFormat::SingleUnderline);
  }

  void NeumaSyntaxHighlighter::highlightBlock(const QString &text)
  {
    foreach (const HighlightingRule &rule, highlightingRules)
      {
        QRegExp expression(rule.pattern);
        int index = expression.indexIn(text);
        while (index >= 0)
        {
          int length = expression.matchedLength();
          setFormat(index, length, rule.format);
          index = expression.indexIn(text, index + length);
        }
      }
    setCurrentBlockState(0);

    int startIndex = -2;
    if (previousBlockState() != 1)
    {
      startIndex = rxErrorInsertedStart.indexIn(text);
      if (startIndex >= 0)
      {
        setCurrentBlockState(1);
        setFormat(startIndex, text.length() - startIndex, fErrorInserted);
      }
    }
    else
    {
      setCurrentBlockState(1);
      setFormat(0, text.length(), fErrorInserted);
    }
  }
}
